package com.example.demo.service.impl;

import com.example.demo.dataobject.BloodSugar;
import com.example.demo.dataobject.BloodSugarRecord;
import com.example.demo.dataobject.UserInfo;
import com.example.demo.repository.BloodSugarRecordRepository;
import com.example.demo.repository.UserInfoRepository;
import com.example.demo.service.BloodSugarRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;

@Service
@Slf4j
public class BloodSugarRecordServiceImpl implements BloodSugarRecordService {

    @Autowired
    private UserInfoRepository userInfoRepository;

    @Autowired
    private BloodSugarRecordRepository bloodSugarRecordRepository;


    @Override
    public BloodSugarRecord save(Double bloodSugar, String telephone, Integer periodType, Date date, Timestamp recordTime, String remarks) {
        if (StringUtils.isEmpty(telephone) || bloodSugar == null) {
            log.info("电话号码或者血糖数值为空");
            return null;
        }
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);
        BloodSugarRecord bloodSugarRecord = new BloodSugarRecord(bloodSugar, userInfo.getId(), periodType, date, recordTime, remarks);
        BloodSugarRecord result = bloodSugarRecordRepository.save(bloodSugarRecord);

        return result;
    }

    @Override
    public List<BloodSugarRecord> findByTelephone(String telephone) {
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);
//        List<BloodSugarRecord> result = bloodSugarRecordRepository.findByTelephoneOrderByRecordTimeDesc(telephone);
        List<BloodSugarRecord> result = bloodSugarRecordRepository.findByUserIdOrderByRecordTimeDesc(userInfo.getId());
        return result;
    }

    @Override
    public BloodSugarRecord findById(Integer id) {
        BloodSugarRecord result = bloodSugarRecordRepository.findById(id);
        return result;
    }

    @Override
    public Boolean delete(Integer id) {
        bloodSugarRecordRepository.delete(id);
        BloodSugarRecord result = findById(id);
        if (result == null) {
            return true;
        }
        return false;
    }

    @Override
    public BloodSugarRecord saveModification(Integer id, Double bloodSugar, String telephone, Integer periodType, Date date, Timestamp recordTime, String remarks) {
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);

        BloodSugarRecord bloodSugarRecord = new BloodSugarRecord(id, bloodSugar, userInfo.getId(), periodType, date, recordTime, remarks);
        BloodSugarRecord result = bloodSugarRecordRepository.save(bloodSugarRecord);

        return result;
    }

    @Override
    public List<BloodSugarRecord> findByDateOrderByRecordTime(Date date) {
        List<BloodSugarRecord> result = bloodSugarRecordRepository.findByDateOrderByRecordTime(date);
        return result;
    }

    @Override
    public List<BloodSugarRecord> findByTelephoneAndDateOrderByRecordTime(String telephone, Date date) {
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);
//        List<BloodSugarRecord> result = bloodSugarRecordRepository.findByTelephoneAndDateOrderByRecordTime(telephone, date);
        List<BloodSugarRecord> result = bloodSugarRecordRepository.findByUserIdAndDateOrderByRecordTime(userInfo.getId(), date);
        return result;
    }

    @Override
    public BloodSugarRecord findLastSugarRecord(String telephone) {
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);
//        List<BloodSugarRecord> result = bloodSugarRecordRepository.findByTelephoneOrderByRecordTimeDesc(telephone);
        List<BloodSugarRecord> result = bloodSugarRecordRepository.findByUserIdOrderByRecordTimeDesc(userInfo.getId());
        System.out.println(result);
        if (result != null && !result.isEmpty()) {
            BloodSugarRecord bloodSugarRecord = result.get(0);
            return bloodSugarRecord;
        } else {

            return null;
        }
    }

    @Override
    public List<BloodSugarRecord> findByUserIdAndDateInAWeek(String telephone, Date date) {
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);
        List<BloodSugarRecord> result = bloodSugarRecordRepository.findByUserIdAndDateAfter(userInfo.getId(), date);
        return result;
    }

    @Override
    public List<BloodSugarRecord> findByUserIdAndDateInAMonth(String telephone, Date date) {
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);
        List<BloodSugarRecord> result = bloodSugarRecordRepository.findByUserIdAndDateAfter(userInfo.getId(), date);
        return result;
    }

    @Override
    public List<BloodSugarRecord> findByUserIdAndDateInThreeMonth(String telephone, Date date) {
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);
        List<BloodSugarRecord> result = bloodSugarRecordRepository.findByUserIdAndDateAfter(userInfo.getId(), date);
        return result;
    }

    @Override
    public List<BloodSugarRecord> findByUserIdAndDateInAPeriod(String telephone, Date date) {
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);
        List<BloodSugarRecord> result = bloodSugarRecordRepository.findByUserIdAndDateAfter(userInfo.getId(), date);
        return result;
    }


}
